package com.vois.jack.btmgr.devices.WL100Dev;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.Arrays;

public class PacketData implements Parcelable {
    final static byte PACKET_HEAD_FLAG = (byte)0X06;
    final static byte PACKET_TAIL_FLAG = (byte)0X07;
    final static byte PACKET_TRAN_FLAG = (byte)0X08;
    final static int MAX_SPP_SEND_ONESIZE = 50;
    final static int MAX_SPP_PACKET_SIZE = 1024;

    public byte mainCmd;
    public byte subCmd;
    public byte[] data;
    public int dataSize;
    public byte packetId;
    public int preSendDataOffset;
    public int sendDataOffset;
    public boolean isPacketValid;


    public PacketData(byte mainCmd, byte subCmd, byte[] data, int dataSize) {
        this.mainCmd = mainCmd;
        this.subCmd = subCmd;
        this.dataSize = dataSize;

        if(data != null) {
            this.data = Arrays.copyOf(data, dataSize);
        }

        packetId = 0;
        preSendDataOffset = 0;
        sendDataOffset = 0;
    }

    public PacketData(Parcel source) {
        mainCmd = source.readByte();
        subCmd = source.readByte();
        dataSize = source.readInt();
        data = new byte[dataSize];
        source.readByteArray(data);
        preSendDataOffset = source.readInt();
        sendDataOffset = source.readInt();
    }

    public PacketData() {
        isPacketValid = false;
    }

    public static final Creator<PacketData> CREATOR = new Creator<PacketData>() {
        @Override
        public PacketData createFromParcel(Parcel source) {
            return new PacketData(source);
        }

        @Override
        public PacketData[] newArray(int size) {
            return new PacketData[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByte(mainCmd);
        dest.writeByte(subCmd);
        dest.writeInt(dataSize);
        dest.writeByteArray(data);
        dest.writeInt(preSendDataOffset);
        dest.writeInt(sendDataOffset);
    }
}

